package com.test.utils.text;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import com.test.entity.User;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

public class XMLUtils {

    /**
     * xml格式化输出
     * @param xmlString  xml字符串
     */
    public static String xmlFormat(String xmlString) throws IOException, SAXException, ParserConfigurationException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        InputSource source = new InputSource(new StringReader(xmlString));
        Document document = documentBuilder.parse(source);
        OutputFormat outputFormat = new OutputFormat(document);
        outputFormat.setLineWidth(100);
        outputFormat.setIndenting(true);
        outputFormat.setIndent(5);
        outputFormat.setEncoding("GBK");
        Writer writer = new StringWriter();
        XMLSerializer xmlSerializer = new XMLSerializer(writer,outputFormat);
        xmlSerializer.serialize(document);
        return writer.toString();
    }

    /**
     * xml装换成JavaBean
     * @param xml  xml字符串
     * @param c    转换成的类型
     */
    public static <T> T xmlToJavaBean(String xml,Class<T> c){
        T t = null;
        try {
            JAXBContext context = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            //noinspection unchecked
            t = (T)unmarshaller.unmarshal(new StringReader(xml));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return t;

    }

    /**
     * JavaBean装换成xml
     * @param obj       java对象
     * @param encoding  编码
     */
    public static String javaBeanToXml(Object obj, String encoding) {
        String result = null;
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
            marshaller.setProperty(Marshaller.JAXB_ENCODING,encoding);
            StringWriter writer = new StringWriter();
            marshaller.marshal(obj,writer);
            result =writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public void testXmlToJavaBean(){
        String buffer = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
                "<user>" +
                "    <age>12</age>" +
                "    <name>张三</name>" +
                "</user>";
        User user = XMLUtils.xmlToJavaBean(buffer, User.class);
        System.out.println(user);
    }

    public void testJavaBeanToXml(){
        User user = new User("张三","12");
        String xml = XMLUtils.javaBeanToXml(user,"UTF-8");
        System.out.println(xml);
    }

    public void testXmlFormat() throws Exception {
        System.out.println(XMLUtils.xmlFormat("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><user><age>12</age><name>张三</name></user>"));
    }
}
